package de.hauck.rtmidlet.model;

import java.util.Vector;

import de.hauck.rtmidlet.utils.Utils;

/**
 * Class for configuration. Here are all configuration parameters stored as
 * JavaBean. At least should...
 * 
 * @author Achim Hauck
 * 
 */
public class Configuration {
	public static final int COLOR = 0x53E63E;
	public static final String RECORD_STORE_STOCKS_NAME = "Stocks";
	public static final String RECORD_STORE_CONFIG_NAME = "RTConfig";

	public static String VERSION = "";
	public static String AUTHOR = "";
	public static String exportURL = "file:///Speicherkarte/RTMidlet.csv";
	public static String exportPath = null;
	public static String importURL = "";
	
	public static final int FILTER_OVERVIEW_YAHOO = -2;
	
	public static String[] CHART_URLS = {
			"http://www.advfn.com/p.php?pid=staticchart&s=<?sym>&p=<?time>&t=23&dm=0&vol=1",
			"http://extis.dab-bank.com/dab_neu/charts/detail_intrachart?type=time&tsOffset=0&sym1=<?sym>&crmbl=&lg=de&ct=de",
			"http://extis.dab-bank.com/dab_neu/charts/detail_intrachart?sym1=<?sym>&type=intra&ct=de&lg=de&height=202",
			"http://www.finanztreff.de/1/chartNG.gfn?u=100&lateIndex=1&overTime=3&chartType=8&time=<?time>&highLow=1&newsType=1&raster=1&transactionVolumeUnit=1<?sym>&width=276&height=240" };
	private static String[] CHART_STOCKTYPENAMES = { "ADVFN", "Times&Sales",
			"Intraday", "Finanztreff" };

	public static final int CONN_STATUS_OFFLINE = 0;
	public static final int CONN_STATUS_CONNECTING = 1;
	public static final int CONN_STATUS_CONNECTED = 2;

	public static int ALERT_WAIT_TIME = 2000;

	public static String[] urlRTTimeIntervalls = { "0", "1", "2", "3", "4", "5" };
	public static String[] urlFINANZTREFFTimeIntervalls = { "0", "8", "100",
			"300", "600", "10000", "30000", "50000", "100000" };

	public static String urlIB = "https://www.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest?t=%token%&q=%query%";
	public static String IB_TOKEN = "";
	public static String[] IB_QUERIES = {"0"};
	
	// config settings
	public static String timeOffset = "6";

	// urls for links in menu
	public static String link1 = "";
	public static String link2 = "";

	// Logging Parameters
	public static int logLevel = 0;
	public static boolean logOn = true;

	// public static String initPath = "";
	private static long downloadCounter = 0;
	private static long downloadSizeTotal = 0;

	public static boolean dirtyFlagStocks = false;
	public static boolean dirtyFlagConfig = false;
	public static boolean fileWritingPossible = true;

	public static long getDownloadCounter() {
		return downloadCounter;
	}

	public static long getDownloadSizeTotal() {
		return downloadSizeTotal;
	}

	public static void setDownloadSizeTotal(long size) {
		downloadSizeTotal = size;
	}

	public static void addToDownloadCounter(long dc) {
		downloadCounter += dc;
		downloadSizeTotal += dc;
		dirtyFlagConfig = true;
	}

	// public static void setInitPath(String path) {
	// initPath = path;
	// }

	public static String getURL(int pos, String symbol) {
		System.out.println("pos: " + pos + " " + symbol);
		String url = CHART_URLS[pos];
		// TODO: FIX Finanztreff
		if (pos == 3) {
			if (symbol.indexOf(".") == -1)
				symbol = "&instrumentId=" + symbol;
			else
				symbol = "&instrumentKey=" + symbol;
		}
		// switch (type) {
		// case CHART_RT: {
		// url = urlRT;
		// break;
		// }
		// case CHART_TS: {
		// url = urlTS;
		// break;
		// }
		// case CHART_INTRADAY: {
		// url = urlINTRADAY;
		// break;
		// }
		// case CHART_FINANZTREFF: {
		// url = urlFINANZTREFF;
		// if (symbol.indexOf(".") == -1)
		// symbol = "&instrumentId=" + symbol;
		// else
		// symbol = "&instrumentKey=" + symbol;
		// break;
		// }
		// }
		return Utils.replace(url, "<?sym>", symbol);
	}

	public static String getIBUrl(String query) {
		String url = Utils.replace(urlIB, "%token%", IB_TOKEN);
		url = Utils.replace(url, "%query%", query);
		return url;
	}

	public static String[][] getAvailableUrls(StockBean selStock) {
		String[][] urls = null;
		Vector urlsVec = new Vector();
		Vector nameVec = new Vector();
		for (int i = 0; i < selStock.getChartSymbols().length; i++) {
			if (null != selStock.getChartSymbol(i)
					&& !selStock.getChartSymbol(i).equals("")) {
				urlsVec.addElement(Utils.replace(getURL(i, selStock
						.getChartSymbol(i)), "<?time>", "0"));
				nameVec.addElement(Configuration.getAvailableChartTypes()[i]);
			}
		}
		System.out.println("vector sizes: " + urlsVec.size() + ", "
				+ nameVec.size());
		if (urlsVec.size() > 0) {
			urls = new String[2][urlsVec.size()];
			for (int i = 0; i < urlsVec.size(); i++) {
				urls[0][i] = (String) urlsVec.elementAt(i);
				urls[1][i] = (String) nameVec.elementAt(i);
			}
		}

		return urls;
	}

	public static String[] getTimeIntervalls(int type) {
		// TODO: no static values!
		switch (type) {
		case 0: {
			return urlRTTimeIntervalls;
		}
		case 3: {
			return urlFINANZTREFFTimeIntervalls;
		}
		}

		return null;
	}

	public static String[] getAvailableChartTypes() {
		return CHART_STOCKTYPENAMES;
	}

	public static void addNewChartLink(String desc, String url) {
		System.out.println("adding as new chartlink: " + desc + " with " + url);
		String[] newCHART_STOCKTYPENAMES = new String[CHART_STOCKTYPENAMES.length + 1];
		String[] newCHART_URLS = new String[CHART_URLS.length + 1];
		for (int i = 0; i < CHART_STOCKTYPENAMES.length; i++) {
			newCHART_STOCKTYPENAMES[i] = CHART_STOCKTYPENAMES[i];
			newCHART_URLS[i] = CHART_URLS[i];
			System.out.println(i + ": copying " + CHART_STOCKTYPENAMES[i]);
			System.out.println(i + ": copying " + CHART_URLS[i]);
		}
		newCHART_STOCKTYPENAMES[newCHART_STOCKTYPENAMES.length - 1] = desc;
		newCHART_URLS[newCHART_URLS.length - 1] = url;
		CHART_STOCKTYPENAMES = newCHART_STOCKTYPENAMES;
		CHART_URLS = newCHART_URLS;
	}

	public static void setAvailableChartTypes(String[] newTypes) {
		CHART_STOCKTYPENAMES = newTypes;
	}

	public static void setValue(String param, String value) {
		// TODO: set values by names???
		if (param.equals("link1")) {
			link1 = value;
		} else if (param.equals("link2")) {
			link2 = value;
		} else if (param.equals("timeOffset")) {
			timeOffset = value;
		} else if (param.equals("urlIB")) {
			urlIB = value;
		} else if (param.equals("IB_TOKEN")) {
			IB_TOKEN = value;
		} else if (param.equals("IB_QUERY")) {
			//IB_QUERY_TODAY = value;
		} else if (param.equals("exportPath")) {
			exportPath = value;
		}
	}
}
